<!-- 新增/编辑机房 -->
<template>
  <Dialogdrawerdefect :ddtvisible.sync="visible" size="720px" @close="handleCancel" :title="curStatus === pageStatus.edit ? `编辑机房【${title}】`:'新建机房'" direction="rtl" :has-modal="false" :modal="false" :close-on-click-modal="true">
    <div class="mianBox cont_box" style="padding-top: 0px !important;" v-loading="loading">
      <!-- <AppInfoTitle>
        <b class="fs-16">{{curStatus === pageStatus.edit ? `编辑机房【${title}】`:'新建机房'}}</b>
      </AppInfoTitle> -->
      <el-form inline class="rule-form" ref="form" :model="ruleForm" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="医院名称:" label-width="110px" prop="hospitalName">
              <el-select size="small" class="param-type_input" v-model="ruleForm.hospitalCode" placeholder="请选择" @change="handleChange('hospital')">
                <el-option
                  v-for="(item,index) in mx_hospitalList"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="医院编码:" label-width="110px" prop="hospitalCode">
              <el-input size="small" v-model="ruleForm.hospitalCode" placeholder="请输入" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="影像区域:" label-width="110px" prop="depArea">
              <template slot="label">
                <span>影像区域:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【影像区域】后会同步修改【时段模板】和【时段号源】对应当前机房的【影像区域】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select
                size="small"
                class="param-type_input"
                v-select-create
                allow-create
                filterable
                default-first-option
                v-model="ruleForm.depArea"
                placeholder="请选择"
                collapse-tags
                @change="handleChange('depArea')">
                <el-option
                  v-for="(item,index) in depAreaOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行科室:" label-width="110px" prop="department">
              <span slot="label">
                执行科室:
                <i class="el-icon-circle-plus add_department" @click="addDepartmentRouter" title="科室设置"></i>
              </span>
              <el-select size="small" class="param-type_input" v-model="ruleForm.department" clearable placeholder="请选择" collapse-tags>
                <el-option
                  v-for="(item,index) in departmentOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="checkGroup">
              <template slot="label">
                <span>检查类型:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="放射，超声，心超等">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select
                size="small"
                class="param-type_input"
                clearable
                v-select-create
                allow-create
                filterable
                default-first-option
                v-model="ruleForm.examType"
                placeholder="请选择"
                collapse-tags
                @change="handleChange('examType')">
                <el-option
                  v-for="(item,index) in examTypeOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="dep">
              <template slot="label">
                <span>检查部门:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【检查部门】后会同步修改【时段模板】和【时段号源】对应当前机房的【检查部门】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select size="small" class="param-type_input" v-select-create allow-create filterable default-first-option v-model="ruleForm.dep" placeholder="请选择" collapse-tags @change="handleChange('dep')">
                <el-option
                  v-for="(item,index) in depOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="modalityType">
              <template slot="label">
                <span>设备类型:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【设备类型】后会同步修改【时段模板】和【时段号源】对应当前机房的【设备类型】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select
                size="small"
                class="param-type_input"
                v-select-create
                allow-create
                filterable
                default-first-option
                v-model="ruleForm.modalityType"
                placeholder="请选择"
                collapse-tags>
                <el-option
                  v-for="(item,index) in modalityTypeOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="modality">
              <template slot="label">
                <span>设备名称:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【设备名称】后会同步修改【时段模板】和【时段号源】对应当前机房的【设备名称】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select size="small" class="param-type_input" v-select-create allow-create filterable default-first-option v-model="ruleForm.modality" placeholder="请选择" collapse-tags>
                <el-option
                  v-for="(item,index) in modalityOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号:" label-width="110px" prop="machineType">
              <el-input size="small" v-model="ruleForm.machineType" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="checkGroup">
              <template slot="label">
                <span>检查分组:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【检查分组】后会同步修改【时段模板】和【时段号源】对应当前机房的【检查分组】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-select size="small" class="param-type_input" clearable v-select-create allow-create filterable default-first-option v-model="ruleForm.checkGroup" placeholder="请选择" collapse-tags>
                <el-option
                  v-for="(item,index) in groupOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机房名称:" label-width="110px" prop="checkRoom">
              <template slot="label">
                <span>机房名称:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="修改【机房名称】后会同步修改【时段模板】和【时段号源】对应当前机房的【机房名称】">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <el-input size="small" v-model="ruleForm.checkRoom" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机房状态:" label-width="110px" prop="checkRoomState">
              <el-select size="small" class="param-type_input" v-model="ruleForm.checkRoomState" placeholder="请选择" collapse-tags>
                <el-option
                  v-for="(item,index) in statusOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="AETITLE:" label-width="110px" prop="aeTitle">
              <el-input size="small" v-model="ruleForm.aeTitle" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="IP地址:" label-width="110px" prop="ipAddress">
              <el-input size="small" v-model="ruleForm.ipAddress" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="MAC地址:" label-width="110px" prop="callMac">
              <el-input size="small" v-model="ruleForm.callMac" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排队号前缀:" label-width="110px" prop="callType">
              <el-input size="small" v-model="ruleForm.callType" clearable placeholder="请输入字母"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机房排序:" label-width="110px" prop="checkRoomOrser">
              <el-input size="small" v-model.number="ruleForm.checkRoomOrser" clearable placeholder="请输入字母"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房间号:" label-width="110px" prop="roomNo">
              <el-input size="small" v-model="ruleForm.roomNo" maxlength="50" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="检查地点:" label-width="110px" prop="modalityAddress">
              <el-select
                class="long-input"
                size="small"
                v-model="ruleForm.modalityAddress"
                v-select-create
                filterable
                allow-create
                clearable
                default-first-option
                placeholder="请选择">
                <el-option
                  v-for="(item,index) in modalityAddressOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="护士站:" label-width="110px" prop="nurseStation">
              <el-input size="small" v-model="ruleForm.nurseStation" maxlength="32" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="医院院区名称:" label-width="110px" prop="areaName">
              <el-input size="small" v-model="ruleForm.areaName" maxlength="50" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="医院院区代码:" label-width="110px" prop="areaNo">
              <el-input size="small" v-model="ruleForm.areaNo" maxlength="32" clearable placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" label-width="110px" prop="isAutoSign">
              <template slot="label">
                <span>自动签到:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="前台登记时当前班次时间内登记数据直接自动签到">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <div style="width: 210px;">
                <el-switch
                  size="small"
                  v-model="ruleForm.isAutoSign"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                >
                </el-switch>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" label-width="120px" prop="isBackMedical">
              <template slot="label">
                <span>回写医院系统:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="200"
                  trigger="hover"
                  content="回写登记数据到第三方医技系统">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <div style="width: 210px;">
                <el-switch
                  size="small"
                  v-model="ruleForm.isBackMedical"
                  :active-value="1"
                  :inactive-value="0"
                  active-color="#13ce66"
                >
                </el-switch>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="" label-width="110px" prop="virtualRoom">
              <template slot="label">
                <span>机房类型:</span>
                <el-popover
                  placement="top-start"
                  title=""
                  width="300"
                  trigger="hover"
                  content="虚拟机房的排队号使用检查分组查询；检查机房、预约池的排队号用机房名称判断">
                  <i class="el-icon-question pointer" slot="reference"></i>
                </el-popover>
              </template>
              <div>
                <el-select size="small" class="param-type_input" v-model="ruleForm.virtualRoom" placeholder="请选择" collapse-tags>
                  <el-option
                    v-for="(item,index) in virtualRoomOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="110px" prop="checkGroup">
              <template slot="label">
                <span>护士站分组:</span>
              </template>
              <el-select
                size="small"
                class="param-type_input"
                multiple
                clearable
                v-select-create
                allow-create
                filterable
                default-first-option
                v-model="ruleForm.haveGroup"
                placeholder="请选择"
                collapse-tags>
                <el-option
                  v-for="(item,index) in groupOptions"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="注意事项:" label-width="110px" prop="examNotice">
              <el-input
                class="long-input"
                type="textarea"
                size="small"
                v-model="ruleForm.examNotice"
                placeholder="请输入"
                :autosize="{ minRows: 3, maxRows: 6}"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <div class="buttons flex flex-row justify-between align-center">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              更多操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="reset">重置表单</el-dropdown-item>
              <el-dropdown-item command="delete" v-if="curStatus === pageStatus.edit">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <el-checkbox
            style="margin-left: 10px;"
            v-if="curStatus === pageStatus.add"
            v-model="defaultChecked"
            @change="setDefaultFormData">赋值默认</el-checkbox>
          <div class="right-buttons">
            <el-button class="floatR m-l-1" type="primary" size="small" @click="handleConfirm(true)" v-if="curStatus === pageStatus.edit">已此为模板新建</el-button>
            <el-button class="floatR m-l-1" type="primary" size="small" @click="handleConfirm(false)">确定</el-button>
            <el-button class="floatR" size="small" @click="handleClose">取消</el-button>
          </div>
        </div>
      </el-form>
    </div>
  </Dialogdrawerdefect>
</template>

<script>
import {
  getDepAreaList,
  getDepList,
  getModalityList,
  getModalityTypeList,
  getItemList,
  getCheckGroupList,
  addCheckroom,
  editCheckroom,
  deleteCheckroom,
  getisexistbyaetitle,
  getlistbymodalityaddress,
  getExamList
} from '@/api/api.js'
import { getLocalStorage } from '@/utils/localStorage'
import Dialogdrawerdefect from '@/components/dialogbox/dialogDrawerDefect'
// import AppInfoTitle from '@/components/appInfoTitle/index'
import { PageStatus } from '@/utils/constant'
const callTypeReg = /^[a-zA-Z]+$/i
export default {
  name: '',
  components: { Dialogdrawerdefect },
  props: {
    showRight: {
      Boolean,
      default: false
    },
    curStatus: {
      String,
      default: ''
    },
    rowData: {
      Object,
      default: () => {}
    },
    formData: {
      Object,
      default: () => {}
    },
    title: {
      String,
      default: ''
    },
    hospitalCode: {
      String,
      default: ''
    }
  },
  data() {
    const callTypeValid = (rule, value, callback) => {
      if (value && !callTypeReg.test(value)) {
        callback(new Error('只能输入字母'))
      }
      callback()
    }
    return {
      pageStatus: PageStatus,
      ruleForm: {
        hospitalCode: '',
        depArea: '',
        department: '',
        dep: '',
        modality: '',
        modalityType: '',
        checkGroup: '',
        checkRoom: '',
        checkRoomState: '正常',
        aeTitle: '',
        modalityAddress: '',
        examNotice: '',
        ipAddress: '',
        callMac: '',
        isAutoSign: 0,
        isBackMedical: 0,
        virtualRoom: 0,
        areaNo: '',
        areaName: '',
        roomNo: '',
        nurseStation: '',
        callType: '',
        checkRoomOrser: '',
        examType: '',
        haveGroup: []
      },
      rules: {
        depArea: { required: true, message: '请选择影像区域', trigger: 'blur' },
        /* department: { required: true, message: '请选择执行科室', trigger: 'blur' }, */
        dep: { required: true, message: '请选择检查部门', trigger: 'blur' },
        modality: { required: true, message: '请选择设备名称', trigger: 'blur' },
        modalityType: { required: true, message: '请选择设备类型', trigger: 'blur' },
        // checkGroup: { required: true, message: '请选择机房分组', trigger: 'blur' },
        aeTitle: { required: true, message: '请输入AETITLE', trigger: 'blur' },
        checkRoom: [
          { required: true, message: '请输入机房名称', trigger: 'blur' }
        ],
        callType: [
          { validator: callTypeValid, message: '请输入字母', trigger: 'blur' }
        ],
        checkRoomState: { required: true, message: '请选择机房状态', trigger: 'blur' },
        modalityAddress: { required: true, message: '请选择检查地点', trigger: 'blur' }
      },
      visible: false,
      depAreaOptions: [],
      depOptions: [],
      modalityOptions: [],
      modalityTypeOptions: [],
      groupOptions: [],
      departmentOptions: [],
      modalityAddressOptions: [],
      examTypeOptions: [],
      virtualRoomOptions: [
        { label: '检查机房', value: 0 },
        { label: '虚拟机房', value: 1 },
        { label: '预约池', value: 2 }
      ],
      loading: false,
      statusOptions: [
        {
          label: '正常',
          value: '正常'
        },
        {
          label: '关闭',
          value: '关闭'
        }
      ],
      // 赋值默认值
      defaultChecked: true
    }
  },
  watch: {
    async visible(val) {
      this.$emit('update:showRight', val)
      if (val) {
        this.ruleForm.hospitalCode = this.hospitalCode
        // await this.$getHospitalList()
        await this.getAllListData()
        this.$nextTick(() => {
          if (this.rowData) {
            this.ruleForm = JSON.parse(JSON.stringify(this.rowData))
            this.ruleForm.haveGroup = this.ruleForm.haveGroup && this.ruleForm.haveGroup != null ? this.ruleForm.haveGroup.split(',') : []
          } else {
            this.setDefaultFormData()
          }
        })
      }
    },
    showRight(val) {
      this.visible = val
    },
    rowData(val) {
      if (val && this.visible) {
        this.ruleForm = JSON.parse(JSON.stringify(val))
        this.ruleForm.haveGroup = this.ruleForm.haveGroup && this.ruleForm.haveGroup != null ? this.ruleForm.haveGroup.split(',') : []
      }
    },
    formData(data) {
      if (data && this.visible) {
        this.setDefaultFormData()
      }
    },
    hospitalCode: {
      handler(val) {
        if (val && this.visible) {
          this.ruleForm.hospitalCode = val
          this.handleChange('hospital')
        }
      },
      immediate: true
    }
  },
  computed: {},
  created() {
    this.$getHospitalList()
  },
  async mounted() {
    this.visible = this.showRight
    // this.ruleForm.hospitalCode = this.$store.state.user.hospitalCode
    // this.getAllListData()
  },
  methods: {
    async $getHospitalList() {
      // console.log('$getHospitalList-1')
      const storageHospital = getLocalStorage('hospitalOptions') && JSON.parse(getLocalStorage('hospitalOptions')) || ''
      // console.warn('storageHospital', storageHospital)
      if (storageHospital && storageHospital.length > 0) {
        this.mx_hospitalList = storageHospital
      } else {
        const hospList = await this.$store.dispatch('user/getHospitalOptions')
        this.mx_hospitalList = hospList
      }
      // console.log('this.mx_hospitalList', this.mx_hospitalList)
    },
    // 设置赋值默认值
    setDefaultFormData() {
      const data = this.formData
      if (data && this.visible && this.defaultChecked && this.curStatus === this.pageStatus.add) {
        this.ruleForm.depArea = data.depArea[0]
        this.ruleForm.dep = data.dep[0]
        this.ruleForm.modality = data.modality[0]
        this.ruleForm.modalityType = data.modalityType[0]
        this.ruleForm.checkRoom = data.checkRoom
        this.ruleForm.examType = data.examType[0]
        this.getDepartment()
        this.getDepOptions()
        this.getModalityOptions()
        this.getModalityTypeOptions()
        this.getExamTypeOptions()
      }
    },
    // 获取下拉数据
    getAllListData() {
      const that = this
      return new Promise(async (resolve, reject) => {
        const res = Promise.all([
          await that.getDepAreaOptions(),
          that.getDepartment(),
          that.getDepOptions(),
          that.getModalityOptions(),
          that.getModalityTypeOptions(),
          that.getCheckGroupOptions(),
          that.getModalityAddressOptions(),
          that.getExamTypeOptions()
        ])
        if (res) {
          resolve(true)
        } else {
          reject(false)
        }
      })
      // try {
      //   await this.getDepAreaOptions(),
      //   this.getDepartment(),
      //   this.getDepOptions(),
      //   this.getModalityOptions(),
      //   this.getModalityTypeOptions(),
      //   this.getCheckGroupOptions(),
      //   this.getModalityAddressOptions(),
      // } catch (err) {
      //   console.log(err)
      // }
    },
    handleClose() {
      this.$emit('update:showRight', false)
      this.handleCancel()
    },
    handleCancel() {
      // 关闭弹窗
      this.$emit('update:rowData', null)
      this.handleReset()
    },
    handleConfirm(addFlag) {
      this.$refs.form.validate((valid, object) => {
        if (valid) {
          this.checkAeTitle(addFlag).then(res => {
            if (this.curStatus === this.pageStatus.add || addFlag) {
              /* 新增保存 */
              this.addSave(addFlag)
            } else {
              /* 编辑保存 */
              this.editSave()
            }
          })
        } else {
          this.timeout01 = setTimeout(() => {
            var isError = document.getElementsByClassName('is-error')
            isError[0].querySelector('input').focus()
          }, 100)
          for (let k in object) {
            this.$message.warning(object[k][0].message)
            return
          }
        }
      })
    },
    handleReset() {
      /* 重置 */
      this.$refs.form.clearValidate()
      this.$refs.form.resetFields()
    },
    delete() {
      /* 删除 */
      this.$confirm('此操作将永久删除该机房, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteData()
      })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    deleteData() {
      deleteCheckroom({ id: this.ruleForm.id, hospitalCode: this.ruleForm.hospitalCode }).then((res) => {
        if (res.code === 10000) {
          this.$message.success('删除成功')
          this.handleClose()
          this.refresh()
        }
      })
    },
    refresh() {
      // 本地缓存排班管理的机房选择数据重置默认选项
      // this.$store.dispatch('user/getDefaultCheckRoomTree')
      // this.$store.dispatch('user/resetDefaultCheckRoom', [])
      this.getAllListData()

      this.$emit('refresh')
    },
    checkAeTitle(addFlag) {
      return new Promise((rs, rj) => {
        let params = {
          hospitalCode: this.ruleForm.hospitalCode,
          aeTitle: this.ruleForm.aeTitle
        }
        if (this.curStatus === this.pageStatus.edit && !addFlag) {
          params.id = this.ruleForm.id
        }
        getisexistbyaetitle(params).then(res => {
          if (res && res.code === 10000 && res.content) {
            this.$confirm('AETITLE重复是否继续', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              rs()
            }).catch(() => {
              rj()
            })
          } else if (res && res.code === 10000 && !res.content) {
            rs()
          }
        }).catch(() => {
          rj()
        })
      })
    },
    addSave(addFlag) {
      /* 新增保存 */
      this.loading = true
      let hospital = this.mx_hospitalList.find((e) => {
        return e.code === this.ruleForm.hospitalCode
      })
      let data = {
        ...this.ruleForm,
        hospitalName: hospital.name,
        haveGroup: this.ruleForm.haveGroup.join(',')
      }
      data.checkRoomOrser = data.checkRoomOrser * 1
      addCheckroom(data).then((res) => {
        this.loading = false
        if (res.code === 10000) {
          if (addFlag) {
            this.$message.success('新增成功，当前编辑机房为：' + this.ruleForm.checkRoom)
            this.ruleForm.id = res.content.id
            this.$emit('update:title', this.ruleForm.checkRoom)
          } else {
            this.$message.success('保存成功')
          }
          this.$emit('refresh')
          this.visible = false
          // this.$emit('refreshTree')
        }
      }).catch(() => {
        // this.loading = true
        this.timeout02 = setTimeout(() => {
          this.loading = false
        }, 200)
      })
    },
    editSave() {
      /* 编辑保存 */
      this.loading = true
      let hospital = this.mx_hospitalList.find((e) => {
        return e.code === this.ruleForm.hospitalCode
      })
      let data = {
        ...this.ruleForm,
        hospitalName: hospital.name,
        haveGroup: this.ruleForm.haveGroup.join(',')
      }
      data.checkRoomOrser = data.checkRoomOrser * 1
      data.virtualRoom = data.virtualRoom ? data.virtualRoom * 1 : 0
      editCheckroom(data).then((res) => {
        this.loading = false
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.$emit('update:title', this.ruleForm.checkRoom)
          this.$emit('refresh')
          this.visible = false
          // this.$emit('refreshTree')
        }
      }).catch(() => {
        // this.loading = true
        this.timeout03 = setTimeout(() => {
          this.loading = false
        }, 500)
      })
      // this.loading = false
    },
    handleCommand(command) {
      switch (command) {
        case 'reset':
          this.handleReset()
          break
        case 'delete':
          this.delete()
          break
        default:
          break
      }
    },
    getDepAreaOptions() {
      return new Promise(async resolve => {
        await getDepAreaList({ hospitalCode: this.ruleForm.hospitalCode }).then(res => {
          if (res.code === 10000) {
            this.depAreaOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getExamTypeOptions() {
      const params = {
        hospitalCode: this.ruleForm.hospitalCode,
        depArea: this.ruleForm.depArea,
        areaName: this.ruleForm.areaName
      }
      return new Promise(resolve => {
        getExamList(params).then(res => {
          if (res.code === 10000) {
            this.examTypeOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getDepOptions() {
      let params = {
        hospitalCode: this.ruleForm.hospitalCode,
        depArea: this.ruleForm.depArea,
        examType: this.ruleForm.examType
      }
      return new Promise(async resolve => {
        await getDepList(params).then(res => {
          if (res.code === 10000) {
            this.depOptions = res.content.list
            if (this.depOptions.length === 1) {
              this.ruleForm.dep = this.depOptions[0]
            }
            resolve(res)
          }
        })
      })
    },
    getModalityOptions() {
      let params = {
        hospitalCode: this.ruleForm.hospitalCode,
        depArea: this.ruleForm.depArea,
        dep: this.ruleForm.dep
      }
      return new Promise(async resolve => {
        await getModalityList(params).then(res => {
          if (res.code === 10000) {
            this.modalityOptions = res.content.list
            if (this.modalityOptions.length === 1) {
              this.ruleForm.modality = this.modalityOptions[0]
            }
            resolve(res)
          }
        })
      })
    },
    getModalityTypeOptions() {
      let params = {
        hospitalCode: this.ruleForm.hospitalCode,
        depArea: this.ruleForm.depArea,
        dep: this.ruleForm.dep,
        examType: this.ruleForm.examType
      }
      return new Promise(async resolve => {
        await getModalityTypeList(params).then(res => {
          if (res.code === 10000) {
            this.modalityTypeOptions = res.content.list
            if (this.modalityTypeOptions.length === 1) {
              this.ruleForm.modalityType = this.modalityTypeOptions[0]
            }
            resolve(res)
          }
        })
      })
    },
    getDepartment() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        // DepArea: this.ruleForm.depArea,
        type: '科室'
      }
      return new Promise(async resolve => {
        await getItemList(data).then(res => {
          if (res && res.code === 10000) {
            this.departmentOptions = res.content.list.map(_item => _item.item)
            // if (this.departmentOptions.length === 1) {
            //   this.ruleForm.department = this.departmentOptions[0]
            // }
            resolve(res)
          }
        })
      })
    },
    getCheckGroupOptions() {
      let data = {
        HospitalCode: this.ruleForm.hospitalCode,
        depArea: this.ruleForm.depArea,
        dep: this.ruleForm.dep,
        examType: this.ruleForm.examType,
        modalityType: this.ruleForm.modalityType
      }
      return new Promise(async resolve => {
        await getCheckGroupList(data).then(res => {
          if (res && res.code === 10000) {
            this.groupOptions = res.content.list
            if (this.groupOptions.length === 1) {
              this.ruleForm.checkGroup = this.groupOptions[0]
            }
            resolve(res)
          }
        })
      })
    },
    async handleChange(val) {
      switch (val) {
        case 'hospital':
          this.depAreaOptions = []
          this.depOptions = []
          this.modalityOptions = []
          this.departmentOptions = []
          this.groupOptions = []
          this.examTypeOptions = []
          this.ruleForm.depArea = ''
          this.ruleForm.department = ''
          this.ruleForm.dep = ''
          this.ruleForm.modality = ''
          this.ruleForm.modalityType = ''
          this.ruleForm.checkGroup = ''
          this.ruleForm.examType = ''
          this.getAllListData()
          break
        case 'depArea':
          // this.departmentOptions = []
          this.depOptions = []
          this.modalityOptions = []
          this.modalityTypeOptions = []
          this.groupOptions = []
          this.examTypeOptions = []
          // this.ruleForm.department = ''
          this.ruleForm.dep = ''
          this.ruleForm.modality = ''
          this.ruleForm.modalityType = ''
          this.ruleForm.checkGroup = ''
          this.ruleForm.examType = ''
          try {
            // await this.getDepartment()
            this.getDepOptions()
            this.getModalityOptions()
            this.getModalityTypeOptions()
            this.getCheckGroupOptions()
            this.getExamTypeOptions()
          } catch (err) {
            console.log(err)
          }
          break
        case 'department':
          this.depOptions = []
          this.modalityOptions = []
          this.modalityTypeOptions = []
          this.groupOptions = []
          this.ruleForm.dep = ''
          this.ruleForm.modality = ''
          this.ruleForm.modalityType = ''
          this.ruleForm.checkGroup = ''

          try {
            await this.getDepOptions()
            this.getModalityOptions()
            this.getModalityTypeOptions()
            this.getCheckGroupOptions()
          } catch (err) {
            console.log(err)
          }
          break
        case 'dep':
          this.modalityOptions = []
          this.modalityTypeOptions = []
          this.groupOptions = []
          this.ruleForm.modality = ''
          this.ruleForm.modalityType = ''
          this.ruleForm.checkGroup = ''

          try {
            await this.getModalityOptions()
            await this.getModalityTypeOptions()
            await this.getCheckGroupOptions()
          } catch (err) {
            console.log(err)
          }
          break
        case 'examType':
          this.modalityOptions = []
          this.modalityTypeOptions = []
          this.groupOptions = []
          this.dep = []
          this.ruleForm.modality = ''
          this.ruleForm.modalityType = ''
          this.ruleForm.checkGroup = ''
          this.ruleForm.dep = ''
          try {
            await this.getModalityOptions()
            await this.getModalityTypeOptions()
            await this.getCheckGroupOptions()
            await this.getDepOptions()
          } catch (err) {
            console.log(err)
          }
          break
        default:
          break
      }
    },
    changeStatus(status) {
      this.ruleForm.checkRoomState = status
    },
    getModalityAddressOptions() {
      getlistbymodalityaddress({ hospitalCode: this.ruleForm.hospitalCode }).then(res => {
        if (res && res.code === 10000) {
          this.modalityAddressOptions = res.content.list
        }
      })
    },
    addDepartmentRouter() {
      this.$router.push({
        path: '/business/checkProject',
        query: {
          type: '科室'
        }
      })
    }
  },
  beforeDestroy() {
    this.timeout01 && clearTimeout(this.timeout01)
    this.timeout02 && clearTimeout(this.timeout02)
    this.timeout03 && clearTimeout(this.timeout03)
  },
  destroyed() {},
  beforeUpdate() {},
  updated() {}
}
</script>
<style lang='scss' scoped>
.add_department{
  font-size: 14px;
  cursor: pointer;
  color: #00c0ef;
  &:hover{
    opacity: 0.6;
  }
}
/deep/ .el-form-item__label{
  text-align: right;
}
.mianBox{
  /deep/ .el-input{
    width: 215px;
  }
  .long-input{
    width: 555px;
    /deep/ .el-input{
        width: 555px;
    }
  }
  .name{
    font-weight: bold;
  }
  .el-form--inline .el-form-item{
    margin-right:0px;
    margin-bottom: 16px;
  }
  /deep/ .el-form-item__label{
    line-height: 32px;
  }
  /deep/ .el-form-item__content{
    line-height: 32px;
  }
  .buttons{
    width: 100%;
    .el-dropdown{
      color: #00c0ef;
    }
  }
  .pointer {
    cursor: pointer;
  }
}
</style>
